<template>
  <div>
    <el-row  type="flex" class="row-bg">
      <el-col>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="搜索">
          <el-input v-model="formInline.searchVal" placeholder="咨询名称"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formInline.selectedVal" clearable placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="findSubmit">查询</el-button>
        </el-form-item>
      </el-form>
      </el-col>
      <el-col><el-button type="primary" @click="addBtn">新建</el-button></el-col>
    </el-row>

    <el-table border  :data="tableData" stripe style="width: 100%">
      <el-table-column align="center" type="index" label="序号"></el-table-column>
      <el-table-column  align="center" prop="title" label="文章标题"> </el-table-column>
      <el-table-column  align="center" prop="name" label="创建人"> </el-table-column>
      <el-table-column  align="center" prop="updateTime" label="创建日期"> </el-table-column>
      <el-table-column  align="center" prop="status" label="状态">
        <el-switch
            slot-scope="scoped"
            :value ="scoped.row.status"
            @change="changeStatus(scoped.row)"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
      </el-table-column>
      <el-table-column align="center" label="操作" width="300">
        <template slot-scope="scoped">
          <el-button @click="editBtn(scoped.row)" size="mini" type="warning">编辑</el-button>
          <el-button @click="delData(scoped.row)" size="mini" type="danger">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row>
      <el-col>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageindex"
          :page-sizes="[5, 10, 15]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-col>
    </el-row>
    <el-dialog
      v-if="dialogVisible"
      :title="dialogType"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="diaLogClose">
      <el-form :model="infoForm" status-icon ref="ruleForm" :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item  label="文章标题" prop="title" >
          <el-input  v-model="infoForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="创建人" prop="name">
          <el-input  v-model="infoForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="创建日期" prop="updateTime">
          <el-input  v-model="infoForm.updateTime" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item  label="状态" prop="status">
          <el-switch
            v-model ="infoForm.status"
            active-color="#13ce66"
            inactive-color="#ff4949">
          </el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="formSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import api from '@/api'
export default {
  data () {
    return {
      options: [{
        value: '开启',
        label: '开启'
      }, {
        value: '关闭',
        label: '关闭'
      }],
      formInline: {
        searchVal: '',
        selectedVal: ''
      },
      dialogVisible: false,
      dialogType: '',
      infoForm: {
        id: '',
        title: '',
        name: '',
        updateTime: '',
        status: false
      },
      rules:{
        title: [
          { required: true , message:'输入不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true , message:'输入不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: true , message:'输入不能为空', trigger: 'blur' }
        ],
        status:[
          { required: true , message:'输入不能为空', trigger: 'blur' }
        ]
      },
      tableData: [],
      total: 0,
      pageindex: 1,
      pagesize: 10
    }
  },
  watch: {
    dialogVisible (newVal) {
      if (!newVal) {
        for (const key in this.infoForm) {
          if (typeof this.infoForm[key] === 'boolean') {
            this.infoForm[key] = false
          } else {
            this.infoForm[key] = ''
          }
        }
      }
    }
  },
  created () {
    this.getList()
  },
  methods: {
    async getList () {
      const { pageindex, pagesize, searchVal: keyWord } = this
      const { data } = await api.getinformationList({ pageindex, pagesize, keyWord })
      if (data.meta.status) {
        this.tableData = data.data.list
        this.total = data.data.total
      }
    },
    async changeStatus (row) {
      const { status: isSwitch, id } = row
      const { data } = await api.editInfoList({ isSwitch: !isSwitch, id })
      if (data.meta.status) {
        row.status = !row.status
        this.$message.success(data.meta.msg)
      } else {
        this.$message.error(data.meta.msg)
      }
    },

    addBtn () {
      this.dialogType = '添加'
      this.dialogVisible = true
    },
    editBtn (row) {
      this.dialogType = '编辑'
      this.dialogVisible = true
      for (const key in row) {
        this.infoForm[key] = row[key]
      }
    },
    async editData () {
      const { infoForm } = this
      const { data } = await api.editInfoList({ infoForm })
      if (data.meta.status) {
        this.$message.success(data.meta.msg)
        this.dialogVisible = false
        this.getList()
      }
    },
    async addData () {
      const { infoForm } = this
      const { data } = await api.addInfoList({ infoForm })
      if (data.meta.status) {
        this.$message.success(data.meta.msg)
        this.dialogVisible = false
        this.getList()
      }
    },
    async diaLogClose () {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.dialogVisible = false
        })
        .catch(_ => {})
    },

    async delData (row) {
      try {
        const { id } = row
        await this.$confirm('确认删除吗？')
        const { data } = await api.deleteInfoList({ id })
        if (data.meta.status) {
          this.$message.success(data.meta.msg)
          this.getList()
        } else {
          this.$message.error(data.meta.msg)
        }
      } catch (error) {
        this.$message.info('取消删除')
      }
    },
    async formSubmit () {
      switch (this.dialogType) {
        case '添加':
          this.$refs.ruleForm.validate((valid)=>{
            if(valid) return this.addData()
            this.$message.error('请检查填写项是否正确')
          })
          break
        case '编辑':
          this.$refs.ruleForm.validate((valid)=>{
             if(valid) return this.editData()
             this.$message.error('请检查填写项是否正确')
          })
          break
      }
    },
    async findSubmit () {},
    handleSizeChange (size) {
      this.pagesize = size
      this.getList()
    },
    handleCurrentChange (index) {
      this.pageindex = index
      this.getList()
    }
  }

}
</script>

<style lang="less" scoped>

</style>
